#include "ecg.h"
#include <QDebug>

ECG::ECG()
{
}

void ECG::addPoint(double x, double y)
{
    t << x;
    s << y;
}

QVector<double> *ECG::getX()
{
    return &t;
}

QVector<double> *ECG::getY()
{
    return &s;
}

QVector<double> *ECG::derivative()
{
    for (int i = 0; i < range; i++)
    {
        der << 0.0;
    }

    double dx = t.at(range * 2) - t.at(0);
    for (int i = range; i < (s.size() - range); i++)
    {
        double dy = s.at(i + range) - s.at(i - range);
        der << dy / dx;
    }

    for (int i = s.size() - range; i < s.size(); i++)
    {
        der << 0.0;
    }

    return &der;
}

QVector<double> *ECG::getDerivative()
{
    return &der;
}

QVector<int> *ECG::beginning()
{
    double max = 0;
    int imax = 0;
    bool ontop = false;
    beginnings.clear();
    for (int i = 0; i < der.size(); i++)
    {
        if (der.at(i) >= threshold)
        {
            ontop = true;
        }
        if (der.at(i) < threshold && !ontop)
        {
            continue;
        }
        if ((der.at(i) < threshold && ontop) || ((i == der.size() - 1) && ontop))
        {
            ontop = false;
            max = 0;
            beginnings << imax;
            continue;
        }
        if (der.at(i) > max)
        {
            max = der.at(i);
            imax = i;
        }
    }

    return &beginnings;
}
